// @(#)mutex.hh
// Time-stamp: <2008-12-14 16:00:56 root>
// Copyright 2008 Julian Qian
// Author: root@localhost.localdomain
// Version: $Id: mutex.hh,v 0.0 2008/11/16 10:07:53 root Exp $
// Keywords: 
//

#ifndef _MUTEX_H
#define _MUTEX_H 1

#include <pthread.h>
#include <semaphore.h>
#include <assert.h>

enum MUTEX_NP {
    FAST, RECU, ECHK
};

#define MUTEX_LOCK(__mut) __mut->lock()
#define MUTEX_UNLOCK(__mut) __mut->unlock()

class MutEX{
public:
    typedef pthread_mutex_t mutex_t;

#define ASSERT(b) assert(b)
    
    MutEX(MUTEX_NP np);
    virtual ~MutEX();
    
    void lock();
    bool trylock();
    void unlock();

    void cwait();
    void signal();
    
    void wait();
    bool trywait();
    void post();
private:
	pthread_mutexattr_t attr;
    mutex_t mutex;
    sem_t sem;
    pthread_cond_t cond;
    int flag;
};

#endif /* _MUTEX_H */

